package com.longcai.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longcai.bean.PageHelper;
import com.longcai.entity.company.ClyjContext;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.ClyjContextService;
import com.longcai.util.OSSUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-04-08
 */
@Controller
@RequestMapping("/clyjContext")
public class ClyjContextController {

    @Autowired
    private ClyjContextService clyjContextService;

    //列表查询
    @GetMapping(value = "getContext")
    @ResponseBody
    public PageTableResponse saveContextSolr(ClyjContext context, PageHelper pageHelper) {
        Page<ClyjContext> page = new Page<ClyjContext>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjContext> wrapper = new QueryWrapper<ClyjContext>();
        if (context.getType() != null && context.getType() != "")
            wrapper.like("type", context.getType());
        IPage<ClyjContext> sysRoleIPage = this.clyjContextService.selectPage(page, wrapper);
        PageTableResponse pageTableResponse = new PageTableResponse(sysRoleIPage.getTotal(),
                sysRoleIPage.getTotal(), sysRoleIPage.getRecords());
        return pageTableResponse;
    }

    @PostMapping(value = "saveClass")
    @ResponseBody
    public boolean saveClass(ClyjContext clyjContext, MultipartFile banner) {
        if (banner != null) {
            //文档上传oss
            try {
                String fileName = banner.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = banner.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/CONTEXT/");
                clyjContext.setType(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (clyjContext.getId() == null || clyjContext.getId() == "" || clyjContext.getId().equals("")) {
            //新增
            clyjContext.setId(UUID.randomUUID().toString());
            clyjContext.setCreateTime(new Date());
            this.clyjContextService.insert(clyjContext);


        } else {
            clyjContext.setUpdateTime(new Date());
            this.clyjContextService.updateById(clyjContext);
        }

        return true;
    }


    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id获取类型")
    @ResponseBody
    public List<ClyjContext> listByBrandId(@PathVariable String id) {
        return clyjContextService.listByBrandId(id);
    }


    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ResponseBody
    public void deleteType(@PathVariable String id) {
        this.clyjContextService.deleteType(id);
    }

    /**
     * 上傳文件
     **/
    @RequestMapping(value = "uploadFile")
    @ResponseBody
    public Map<String, Object> uploadFile(MultipartFile file) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (file != null) {
            //文档上传oss
            try {
                String fileName = file.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = file.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/CONTEXT/");
                resultMap.put("code", 0);
                uploadByInputStream.put("src", uploadByInputStream.get("path"));
                resultMap.put("data", uploadByInputStream);
                return resultMap;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return resultMap;
    }
}


